package com.gem.eolane.service;



import com.gem.eolane.entity.Admin;
import com.gem.eolane.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: jzhang
 * @Date: 2018/10/15 15:51
 * @Description: 自定义用户登录
 */
@Service
public class CustomUserService implements UserDetailsService { //自定义UserDetailsService 接口

    @Autowired
    private AdminMapper adminMapper;

    public UserDetails loadUserByUsername(String username) {
        Admin admin = adminMapper.queryAdmin(username);
        if (admin != null) {
                List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("admin");
                    grantedAuthorities.add(grantedAuthority);
                return new User(admin.getAdminNumber(), admin.getPassword(), grantedAuthorities);
        } else {
            throw new UsernameNotFoundException("账户：" + username + " do not exist!");
        }
    }
}
